﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nyxwin.Main.Base.ViewModels;
using nyxwin.Data.Mail;
using nyxwin.Common.Settings;

namespace nyxwin.Main.ViewModel.SubModels
{
    public class NewMailViewModel : BaseViewModel
    {
        private static readonly Guid SENDMESSAGE_ID = Guid.NewGuid();

        private string _recipient;

        public NewMailViewModel()
        {
            this._content = string.Empty;
            this._recipient = string.Empty;
        }

        public string Recipient
        {
            get { return _recipient; }
            set
            {
                _recipient = value;
                RaisePropertyChanged(() => Recipient);
            }
        }

        private string _content;

        public string Content
        {
            get { return _content; }
            set
            {
                _content = value;
                RaisePropertyChanged(() => Content);
            }
        }

        public string SendButtonTitle
        {
            get
            {
                return Localization.GetString("Send");
            }
        }

        public string CancelButtonTitle
        {
            get
            {
                return Localization.GetString("Cancel");
            }
        }

        protected override void OnLocalizationChanged()
        {
            base.OnLocalizationChanged();

            RaisePropertyChanged(() => SendButtonTitle);
            RaisePropertyChanged(() => CancelButtonTitle);
        }

        public void Send()
        {
            if (string.IsNullOrEmpty(Recipient))
            {
                RaiseError(Localization.GetErrorString("RecipientEmptyError"));
            }
            else if (string.IsNullOrEmpty(Content))
            {
                RaiseError(Localization.GetErrorString("MessageEmptyError"));
            }
            else
            {
                MailManager mgr = new MailManager(ApplicationSettings.Instance.AuthorizationNick, ApplicationSettings.Instance.AuthorizationToken);
                mgr.SendMailCompleted += new EventHandler<SendMailEventArgs>(mgr_SendMailCompleted);
                mgr.ErrorOccurred += new EventHandler<Data.Base.ErrorOccurredEventArgs>(mgr_ErrorOccurred);

                SetIsInDeterminate(SENDMESSAGE_ID, Localization.GetString("SendingMessage"));

                mgr.SendMail(Recipient, Content);
            }
        }

        void mgr_ErrorOccurred(object sender, Data.Base.ErrorOccurredEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                UnSetInDeterminate(SENDMESSAGE_ID);
                RaiseError(e.ErrorMessage);
            });
        }

        void mgr_SendMailCompleted(object sender, SendMailEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                UnSetInDeterminate(SENDMESSAGE_ID);

                if (e.Error)
                {
                    RaiseError(e.ErrorMessage);
                }
                else if (e.Result)
                {
                    GoBack();
                }
            });
        }

        internal void Cancel()
        {
            GoBack();
        }
    }
}
